package com.gsdse.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gsdse.pojo.Department;
import com.gsdse.pojo.ReturnData;
import com.gsdse.pojo.User;
import com.gsdse.service.DepartmentService;
import com.gsdse.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;


    //部门 认证码、密钥、公告、祝贺语 等修改
    @RequestMapping("/updateDepartment")
    public ReturnData updateDepartment(Department department) {
        ReturnData res = new ReturnData();
        Integer departmentId = department.getDepartmentId();
        if (departmentService.selectDepartmentById(departmentId) != null) {
            Boolean update = departmentService.updateDepartmentById(department);
            if (update) {
                res.setCode(0);
                res.setMsg("修改成功");
                return res;
            } else {
                res.setCode(1);
                res.setMsg("修改失败");
                return res;
            }
        } else {
            res.setCode(2);
            res.setMsg("departmentId不存在");
            res.setData(department);
            return res;
        }

    }


    //查询 部门认证码，公告，祝贺语
    @RequestMapping("/selectDepartment")
    public ReturnData selectDepartment(String departmentId) {
        ReturnData res = new ReturnData();
        if (StringUtils.isNotEmpty(departmentId)) {
            Department department = departmentService.selectDepartmentById(Integer.parseInt(departmentId));
            if (department == null) {
                res.setCode(1);
                res.setMsg("查询错误，结果为空");
                return res;
            } else {
                res.setCode(0);
                res.setMsg("查询成功");
                res.setData(department);
                return res;
            }
        } else {
            res.setCode(2);
            res.setMsg("为空");
            if (departmentId == null) {
                res.setData("没看到参数");
            } else {
                res.setData(departmentId);
            }
            return res;
        }
    }


    //部门认证，用户加入部门
    //key是密钥
    //identification是认证码，用于认证部门
    //此处departmentId是用户选择要加入的部门
    @RequestMapping("/joindepartment")
    public ReturnData joindepartment(String userId, String identification, String departmentId, String position) {
        ReturnData res = new ReturnData();
        if (StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(identification) && StringUtils.isNotEmpty(departmentId) && StringUtils.isNotEmpty(position)) {
            Department department = departmentService.selectDepartmentById(Integer.parseInt(departmentId));
            String real_identification = department.getIdentification();
            User user = userService.selectUserByUserId(Integer.parseInt(userId));
            if (real_identification.equals(identification)) {
                user.setPosition(position);
                user.setDepartmentId(Integer.parseInt(departmentId));
                userService.updateUserById(user);
                res.setCode(0);
                res.setMsg("认证成功，成功加入部门");
                return res;
            } else {
                res.setCode(1);
                res.setMsg("认证码不对哦~");
                return res;
            }
        } else {
            res.setCode(2);
            res.setMsg("空");
            return res;
        }


    }

    //分页查询部门成员
    @GetMapping("/selectMembers")
    public ReturnData selectMembers(@RequestParam ("departmentId") Integer departmentId ,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        if(departmentId==null){
            ReturnData returnData = new ReturnData();
            returnData.setCode(1);
            returnData.setMsg("传参错误！");
            return returnData;
        }
        return departmentService.selectMembers(departmentId,page,pageSize);
    }

    //查询部门总人数
    @GetMapping("/getTotal")
    public ReturnData getTotal(@RequestParam ("departmentId") Integer departmentId)
    {
        if(departmentId==null){
            ReturnData returnData = new ReturnData();
            returnData.setCode(1);
            returnData.setMsg("传参错误！");
            return returnData;
        }
        return departmentService.getTotal(departmentId);
    }

    @GetMapping("/removeUser")
    //移除部门成员
    public ReturnData removeUser(@RequestParam("userId") Integer userId , @RequestParam("departmentId") String departmentId) {
        if(userId==null){
            ReturnData returnData = new ReturnData();
            returnData.setCode(1);
            returnData.setMsg("传参错误！");
            return returnData;
        }

        return departmentService.removeUser(userId,departmentId);
    }
}
